"use strict";

const Controller = require("../common/admin");

class ShopController extends Controller {
  tableName = "shop";
  //后台注册店铺
  async addShop() {
    let {
      shop_name,
      name,
      telephone,
      auth_card_no,
      auth_card_hand,
      auth_card_front,
      auth_card_back,
      username,
      password,
    } = this.ctx.request.body;
    console.log(shop_name, name, telephone);
    await this.add({ shop_name, name, telephone });
    let siteId = await this.consult({ shop_name });
    let site_id = siteId[0].site_id;
    this.tableName = "member_auth";
    await this.add({
      site_id,
      auth_card_no,
      auth_card_hand,
      auth_card_front,
      auth_card_back,
      status: 1,
      create_time: Date.now(),
    });
    this.tableName = "user";
    await this.add({ username, password, site_id });
    this.resData(0, "会员列表获取成功", null);
  }
  // 查看店铺信息;
  async shopInfrom() {
    this.tableName = "member_auth";
    let memberAuthlist = await this.consult();
    let ps = [];
    memberAuthlist.forEach((item) => {
      this.tableName = "shop";
      let shopinfrom = this.consult({ site_id: item.site_id });
      ps.push(shopinfrom);
    });
    const shopList = await Promise.all(ps);

    let result = memberAuthlist.map((item, index) => ({
      ...item,
      ...shopList[index][0],
    }));
    this.resData(0, "会员列表获取成功", result);
  }
  
  async memberAuthlist() {
    this.tableName = "member_auth";
    let site_id = this.ctx.request.body.siteId;
    let memberAuth = await this.consult({ site_id });
    let ps = [];
    memberAuth.forEach((item) => {
      this.tableName = "shop";
      let shopinfrom = this.consult({ site_id: item.site_id });
      ps.push(shopinfrom);
    });
    const shopList = await Promise.all(ps);
    let result = memberAuth.map((item, index) => ({
      ...item,
      ...shopList[index][0],
    }));
    this.resData(0, "会员列表获取成功", result);
  }
  // 通过审核
  async passAudit(){
    this.tableName = "member_auth";
    let {site_id,status} = this.ctx.request.body
    let result = await this.update({ status },{site_id})
    console.log(site_id,status);
    this.resData(0, "会员列表获取成功", result);
  }
}

module.exports = ShopController;
